module.exports = function(sequelize, DataTypes) {
  var helper = require('../lib/merge')
  var rating = require('./mixins/RatingMixin')

  var User = sequelize.define(
    'User',
    helper.mergeWithArray(
      {
        source: {
          type: DataTypes.ENUM('wechat', 'email'),
          comments: '用户来源'
        },
        openId: {
          type: DataTypes.STRING(100),
          unique: true
        },
        unionId: {
          type: DataTypes.STRING(100),
          unique: true
        },
        accessToken: {
          type: DataTypes.STRING
        },
        refreshToken: {
          type: DataTypes.STRING
        },
        clientVersion: {
          type: DataTypes.STRING(20)
        },
        email: {
          type: DataTypes.STRING(100)
        },
        ipAddress: {
          type: DataTypes.STRING(50)
        },
        name: {
          type: DataTypes.STRING(50)
        },
        password: {
          type: DataTypes.STRING(64)
        },
        phone: {
          type: DataTypes.STRING(20)
        },
        avatarUrl: {
          type: DataTypes.STRING
        },
        roles: {
          type: DataTypes.STRING
        },
        birthday: {
          type: DataTypes.DATEONLY
        },
        country: {
          type: DataTypes.STRING(40)
        },
        province: {
          type: DataTypes.STRING(40)
        },
        city: {
          type: DataTypes.STRING(40)
        },
        language: {
          type: DataTypes.STRING(20)
        },
        nickName: {
          type: DataTypes.STRING(20)
        },
        gender: {
          type: DataTypes.INTEGER(1),
          default: 0
        },
        tokenExpireAt: {
          type: DataTypes.DATE
        },
        data: {
          type: DataTypes.JSON
        },
        unread: {
          type: DataTypes.INTEGER,
          default: 0
        },
        state: {
          type: DataTypes.ENUM('normal')
        },
        lastLoginAt: {
          type: DataTypes.DATE
        },
        profile: {
          type: DataTypes.STRING(140)
        },
        commentedCount: {
          type: DataTypes.INTEGER,
          defaultValue: 0,
          comment: 'Total commented number of this document'
        },
        favoritedCount: {
          type: DataTypes.INTEGER,
          defaultValue: 0,
          comment: 'Total favorited number of this document'
        }
      },
      rating(DataTypes).attributes
    ),
    {
      tableName: 'users',
      paranoid: true,
      indexes: [
        {
          name: 'openId_idx',
          fields: ['openId']
        },
        {
          name: 'unionId_idx',
          fields: ['unionId']
        },
        {
          name: 'email_idx',
          fields: ['email']
        },
        {
          name: 'phone_index',
          fields: ['phone']
        },
        {
          name: 'name_idx',
          fields: ['name']
        },
        {
          name: 'nickName_idx',
          fields: ['nickName']
        }
      ],
      comments: '用户信息表'
    }
  )
  User.associate = function(models) {
    this.belongsToMany(models.Group, {
      as: 'Groups',
      through: 'UserGroup',
      foreignKey: 'userId'
    })
    this.hasMany(models.Answer, {
      foreignKey: 'userId'
    })
    this.belongsToMany(models.User, {
      as: 'RelationUsers',
      through: 'UserRelation',
      foreignKey: 'userId'
    })
    this.belongsToMany(models.User, {
      as: 'RelationTargets',
      through: 'UserRelation',
      foreignKey: 'targetId'
    })
  }
  return User
}
